#!/bin/bash

if [ -d "$2" ]; then
	src_dir=$2
elif [ -d "../test" ]; then
	src_dir="../test"
else
	echo "No source dir!!!"
	exit 1
fi

if [ -d "$3" ]; then
	bin_dir=$3
elif [ -d "../bin" ]; then
	bin_dir="../bin"
else
	echo "No class dir!!!"
	exit 2
fi

if [ -d "$4" ]; then
	lib_dir=$4
elif [ -d "../lib" ]; then
	lib_dir="../lib"
else
	echo "No library dir!!!"
	exit 3
fi

if [ ! -e "$lib_dir/junit-4.12.jar" ]; then
	echo "Missing junit-4.12.jar"
	exit 4
elif [ ! -e "$lib_dir/hamcrest-core-1.3.jar" ]; then
	echo "Missing hamcrest-core-1.3"
	exit 5
elif [ ! -e "$lib_dir/sqljet-1.1.10.jar" ]; then
	echo "Missing sqljet-1.1.10.jar"
	exit 6
else
	echo "Source dir: $src_dir"
	echo "Class dir: $bin_dir"
	echo "Library dir: $lib_dir"
fi

case "$1" in
0)
	echo "Run all tests"
	javac -cp "$lib_dir/hamcrest-core-1.3.jar;$lib_dir/junit-4.12.jar;$bin_dir" -d $bin_dir $src_dir/datastruct/RadixTreeTest.java
	javac -cp "$lib_dir/hamcrest-core-1.3.jar;$lib_dir/junit-4.12.jar;$lib_dir/sqljet-1.1.10.jar;$bin_dir" -d $bin_dir $src_dir/logparser/TestParser.java
	echo "Compiled"
	cd $bin_dir
	java -cp ".;$lib_dir/hamcrest-core-1.3.jar;$lib_dir/junit-4.12.jar" org.junit.runner.JUnitCore datastruct.RadixTreeTest
	java -cp ".;$lib_dir/hamcrest-core-1.3.jar;$lib_dir/junit-4.12.jar;$lib_dir/sqljet-1.1.10.jar" org.junit.runner.JUnitCore logparser.TestParser
	;;
1)
	echo "Run datastruct tests"
	javac -cp "$lib_dir/hamcrest-core-1.3.jar;$lib_dir/junit-4.12.jar;$bin_dir" -d $bin_dir $src_dir/datastruct/RadixTreeTest.java
	echo "Compiled"
	cd $bin_dir
	java -cp ".;$lib_dir/*" org.junit.runner.JUnitCore datastruct.RadixTreeTest
	;;
2)
	echo "Run logparser tests"
	javac -cp "$lib_dir/hamcrest-core-1.3.jar;$lib_dir/junit-4.12.jar;$lib_dir/sqljet-1.1.10.jar;$bin_dir" -d $bin_dir $src_dir/logparser/TestParser.java
	echo "Compiled $?"
	cd $bin_dir
	java -cp ".;$lib_dir/*" org.junit.runner.JUnitCore logparser.TestParser
	;;
*)
	echo "Wrong test number. Use 0-2 numbers."
	exit 7
	;;
esac

exit 0
